package com.meihua.shellapp.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aspsine.irecyclerview.IViewHolder;
import com.meihua.shellapp.R;
import com.meihua.shellapp.bean.ZhongAnBean;
import com.meihua.shellapp.utils.PicassoUtils;
import com.meihua.shellapp.view.FullRoundEdgeImageView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


public class ZhongAnHomeRVAdapter extends RecyclerView.Adapter<IViewHolder> {
    private List<ZhongAnBean.DataBean> mData;

    public ZhongAnHomeRVAdapter(List<ZhongAnBean.DataBean> urls) {
        mData = urls;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    private OnItemClickListener mOnItemClickListener;

    @Override
    public IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home, parent, false));
    }

    @Override
    public void onBindViewHolder(final IViewHolder holder, int position) {
        ((ViewHolder) holder).setData(mData.get(position),position);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final int position = holder.getIAdapterPosition();
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(position, v);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    class ViewHolder extends IViewHolder {
        @Bind(R.id.imgView_photo)
        FullRoundEdgeImageView product_img;
        @Bind(R.id.txt_kind)
        TextView product_name;
        @Bind(R.id.txt_Age)
        TextView age;
        @Bind(R.id.txt_Crowd)
        TextView fit_member;
        @Bind(R.id.txt_Deadline)
        TextView insure_period;
        @Bind(R.id.txt_money)
        TextView txt_money;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
        int[] imgs = {R.drawable.t1,R.drawable.t2,R.drawable.t3,R.drawable.t4,R.drawable.t5,R.drawable.t6,R.drawable.t7};
        public void setData(ZhongAnBean.DataBean mData, int position) {
            PicassoUtils.getInstance().bindImageData(product_img, mData.pic);
            //temp
//            Picasso.with(this.itemView.getContext()).load(mData.pic).into(product_img);

            product_name.setText(mData.name);
            age.setText("承保年龄:" + mData.age);
            if (null == mData.peop) {
                fit_member.setVisibility(View.GONE);
            } else {
                fit_member.setText("适用人群:" + (CharSequence) mData.peop);
            }
            product_name.setText(mData.name);
            insure_period.setText("保险期限:" + mData.time);
            txt_money.setText(mData.money + "元");
        }
    }
}
